package _binary_search;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * 981. 基于时间的键值存储
 */
public class No981 {
    private final Map<String, TreeMap<Integer, String>> db;

    public No981() {
        db = new HashMap<>();
    }

    public void set(String key, String value, int timestamp) {
        TreeMap<Integer, String> treeMap = db.getOrDefault(key, new TreeMap<>());
        treeMap.put(timestamp, value);
        db.put(key, treeMap);
    }

    public String get(String key, int timestamp) {
        TreeMap<Integer, String> treeMap = db.get(key);
        if (Objects.isNull(treeMap)) return "";
        Map.Entry<Integer, String> entry = treeMap.floorEntry(timestamp);
        return Objects.isNull(entry) ? "" : entry.getValue();
    }
}
